import { ESceneEvent } from "src/Game/MesEvent/ESceneEvent";
import { EUIEvent } from "src/Game/MesEvent/EUIEvent";
import CombatantManageBase from "src/Ldz_GameCore/FSM_Animation/FSM_Base/CombatantManageBase";
import BuffManager from "src/Ldz_GameCore/GameBuffManager/BuffBase/Manager/BuffManager";
import TriggerCollider from "src/Ldz_GameCore/GeneralScripts/TriggerCollider";
import CameraControl from "src/Ldz_GameCore/PlayerCore/CameraControl";
import FindPathPoxy from "src/Ldz_GameCore/PlayerCore/MyAStar/FindPathPoxy/FindPathPoxy";
import ResourcePropsManager from "src/Ldz_GameCore/ResourceProps/ResourcePropsManager";
import GlobalD3Environment from "src/_T/D3/scene/GlobalD3Environment";
import MesManager from "src/_T/Mes/MesManager";
import ResLoad from "src/_T/Res/ResLoad";
import { CameraUtils } from "src/_T/Utils/CameraUtils";
import HoneyBeesAttributeBase from "./HoneyBeesAttributeBase";

export default class HoneyBeesBase extends Laya.Script3D {

    /**战斗属性 */
    protected m_AttributeBase: HoneyBeesAttributeBase = new HoneyBeesAttributeBase();
    /**临时战斗属性 */
    m_TempAttributeBase: HoneyBeesAttributeBase = new HoneyBeesAttributeBase();
    /**战斗单位 */
    m_SceneSprite3d: Laya.Sprite3D;
    /**战斗单位的状态机 */
    m_AnimatorManager: CombatantManageBase;             //状态机
    /**寻路组件 */
    m_FinderAgent: FindPathPoxy;
    /**移动路径 */
    m_AIPathArray: Laya.Vector3[] = [];
    /**自身脚本的引用 */
    m_SoldiersBase: HoneyBeesBase;
    /**是否开始战斗 */
    m_IsBattle: boolean = false;
    /**Buff管理器 */
    m_BuffManager: BuffManager;
    /**是否战斗中 */
    IsFighting: boolean = false;
    /**是否死亡 */
    IsDie = false;

    /**是否为PK场景 */
    IsPKScene: boolean = false;

    UpPos: Laya.Vector3 = new Laya.Vector3(0, 1, 0);
    //m_HP_UI: FGUI_Hp_Bar;

    public constructor() {
        super();
    }

    onAwake() {
        // let KK = new Date;
        // console.log(KK.getTime());

        // this.m_HP_UI = FGUI_Hp_Bar.createInstance();
        //this.m_HP_UI.visible = false;
        this.m_SceneSprite3d = this.owner as Laya.Sprite3D;
        this.m_SoldiersBase = this.m_SceneSprite3d.getComponent(HoneyBeesBase);
        this.m_BuffManager = new BuffManager();
        this.m_BuffManager.InitBuffManager(this.m_SceneSprite3d, this.m_SoldiersBase);
    }
    /**移动到某个点 */
    MoveToPos(TargetPos: Laya.Vector3) {
        TargetPos.y = this.m_SceneSprite3d.transform.position.y;
        this.m_SceneSprite3d.transform.lookAt(TargetPos, new Laya.Vector3(0, 1, 0));
        let Fo = new Laya.Vector3();
        this.m_SceneSprite3d.transform.getForward(Fo);
        Laya.Vector3.normalize(Fo, Fo);
        let Pos = new Laya.Vector3(0, 0, 0);
        Laya.Vector3.scale(Fo, ((Laya.timer.delta / 1000) * this.m_TempAttributeBase.m_Speed), Pos);
        //Pos.y = 0;
        Laya.Vector3.add(this.m_SceneSprite3d.transform.position, Pos, Pos);
        this.m_SceneSprite3d.transform.position = Pos.clone();
        this.m_SceneSprite3d.transform.localRotationEulerY += 180;
    }
    /**旋转到目标坐标 */
    RoteteToTarget(TargetPos: Laya.Vector3) {
        let _renderDir: Laya.Vector3 = TargetPos;
        _renderDir.x *= -1;
        let RenderDir: Laya.Quaternion = new Laya.Quaternion(0, 0, 0, 0);
        Laya.Quaternion.rotationLookAt(_renderDir, new Laya.Vector3(0, 1, 0), RenderDir);
        let Ro = new Laya.Quaternion();
        Laya.Quaternion.lerp(this.m_SceneSprite3d.transform.rotation, RenderDir, 0.2, Ro);
    }
    /**攻击目标 */
    AttackTarget(_TargetObj: Laya.Sprite3D): boolean {

        this.m_BuffManager.BuffOnHit();
        let Tar: HoneyBeesBase = _TargetObj.getComponent(HoneyBeesBase);
        if (Tar != null && !Tar.destroyed) {
            if (Tar.BeAttacked(this.m_SoldiersBase)) {
                return true;
            }
        }
        return false;
    }

    /**增加生命值 */
    AddHitPoints(Tag: HoneyBeesBase) {
        this.m_AttributeBase.m_Hitpoints += Tag.m_TempAttributeBase.m_Damage;
        if (this.m_AttributeBase.m_Hitpoints > this.m_AttributeBase.m_MaxHitpoints) {
            this.m_AttributeBase.m_Hitpoints = this.m_AttributeBase.m_MaxHitpoints;
        }
    }

    /**被攻击 */
    BeAttacked(Tag: HoneyBeesBase, BuffBase: any = null): boolean {
        if (this.IsDie) return true;
        this.m_BuffManager.BuffBeHurt();
        if (BuffBase != null) {
            this.m_BuffManager.AddBuffState(BuffBase, Tag);
        }
        this.m_AttributeBase.m_Hitpoints -= Tag.m_TempAttributeBase.m_Damage;
        if (this.m_AttributeBase.m_Hitpoints <= 0) {
            this.IsDie = true;
            Tag.m_BuffManager.BuffAfterKilled();
            this.m_BuffManager.BuffBeforeKilled();
            this.SkillSelef();
            // this.m_HP_UI.visible = false;
            return true;
        } else {
            // if (this.m_HP_UI.parent == null) {
            //     if (MainProxy.instance.Isshow) {
            //         MainProxy.instance.ui.addChild(this.m_HP_UI);
            //     } else if (FightingPanelProxy.instance.isShow) {
            //         FightingPanelProxy.instance.ui.addChild(this.m_HP_UI);
            //     }
            // }
            // if (this.m_HP_UI.parent != null) {
            //     this.m_HP_UI.visible = true;
            //     this.IsCloseUITimne = 5;
            //     let Pos = new Laya.Vector3();
            //     CameraUtils.WorldToScreen2(GlobalD3Environment.Camera, this.m_SceneSprite3d.transform.position, Pos);
            //     this.m_HP_UI.setXY((Pos.x - 70), (Pos.y - 100));
            //     this.m_HP_UI.value = Math.floor((this.m_TempAttributeBase.m_Hitpoints / this.m_TempAttributeBase.m_MaxHitpoints) * 100);
            // }
        }
        return false;
    }

    /**杀死自己 */
    SkillSelef() {
        if (this.owner != null && !this.owner.destroyed) {
            this.owner.removeSelf();
            this.owner.destroy();
        }
    }

    /**销毁 */
    onDestroy() {

    }
    /**更新执行 */
    onUpdate() {
        if (this.m_SceneSprite3d.transform.position.y < 0) {
            this.m_SceneSprite3d.transform.position.y = 4;
        }
        this.m_TempAttributeBase = this.m_AttributeBase.Clone();
        this.m_BuffManager.BuffUpdate();
        this.UpdateAttributeBase();
        this.m_AnimatorManager.onUpdate();
        this.SetHP_UI_Pos();
    }

    IsCloseUITimne: number = 0;
    SetHP_UI_Pos() {
        this.IsCloseUITimne -= (Laya.timer.delta / 1000);
        if (this.IsCloseUITimne < 0) {
            this.IsCloseUITimne = 0;
            // if (this.m_HP_UI != null && this.m_HP_UI.visible) {
            //     this.m_HP_UI.visible = false;
            // }
        }
        // if (this.m_HP_UI != null && this.m_HP_UI.visible) {
        //     let Pos = new Laya.Vector3();
        //     CameraUtils.WorldToScreen2(GlobalD3Environment.Camera, this.m_SceneSprite3d.transform.position, Pos);
        //     this.m_HP_UI.setXY((Pos.x - 70), (Pos.y - 100));
        //     this.m_HP_UI.value = Math.floor((this.m_TempAttributeBase.m_Hitpoints / this.m_TempAttributeBase.m_MaxHitpoints) * 100);
        // }
    }

    /**更新计算属性 */
    UpdateAttributeBase() {
        this.m_TempAttributeBase.m_Damage = this.m_TempAttributeBase.m_Damage;
        this.m_TempAttributeBase.m_HitSpeed = (1 / this.m_TempAttributeBase.m_HitSpeed);
        this.m_TempAttributeBase.m_Hitpoints = this.m_TempAttributeBase.m_Hitpoints;
        this.m_TempAttributeBase.m_Range = this.m_TempAttributeBase.m_Range / 3;
        this.m_TempAttributeBase.m_SoldierName = this.m_TempAttributeBase.m_SoldierName;
        this.m_TempAttributeBase.m_Speed = this.m_TempAttributeBase.m_Speed;
        this.m_TempAttributeBase.m_SoliderChess = this.m_TempAttributeBase.m_SoliderChess;
        this.m_TempAttributeBase.m_SkillId = this.m_TempAttributeBase.m_SkillId;
        this.m_TempAttributeBase.m_Attacksequence = this.m_TempAttributeBase.m_Attacksequence;
    }

    /**获取目标 */
    GetTargetByMap(IsLoop: boolean): any {
        return;
    }
}